import express from 'express'
import cors from 'cors'
import morgan from 'morgan'

import { dataSource } from './db/index.js'

import router from './controller/index.js'

import { initTasks } from './tasks/index.js'

// 加载配置
import dotenv from 'dotenv'

dotenv.config({
  path: `.env.${process.env.NODE_ENV}`
})

const app = express()

try {
  await dataSource.initialize()

  console.log('数据库连接成功')

  app.use(cors())

  app.use(morgan('dev'))

  app.use(express.json())

  app.use(router)

  // 定时任务
  initTasks()

  app.listen(process.env.server_port, () => {
    console.log(`http://localhost:${process.env.server_port}`)
  })
} catch (error) {
  console.log('goldService 启动失败')
  console.log(error)
}
